﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebMVC.Controllers
{
    public class GiangDayController : Controller
    {
        databaseDataContext db = new databaseDataContext();
        //
        // GET: /GiangDay/
        [Authorize(Roles = "0")]
        public ActionResult Index()
        {
            ViewData["Items"] = from h in db.GiangDays
                                select h;
            return View();
        }

        //
        // GET: /GiangDay/Details/5
        [Authorize(Roles = "0")]
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /GiangDay/Create
        [Authorize(Roles = "0")]
        public ActionResult Create()
        {
            IEnumerable<SelectListItem> listGiangVien = db.GiangViens.Select(c => new SelectListItem
            {
                Value = c.id.ToString(),
                Text = c.tengv.ToString()
            });
            IEnumerable<SelectListItem> listMonHoc = db.MonHocs.Select(c => new SelectListItem
            {
                Value = c.id.ToString(),
                Text = c.tenmh.ToString()
            });
            ViewData["ListGiangVien"] = listGiangVien;
            ViewData["ListMonHoc"] = listMonHoc;
            return View();
        }

        //
        // POST: /GiangDay/Create
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Create(GiangDay GiangDay)
        {
            try
            {
                // TODO: Add insert logic here
              
                db.GiangDays.InsertOnSubmit(GiangDay);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {                
                return View();
            }
        }

        //
        // GET: /GiangDay/Edit/5
        [Authorize(Roles = "0")]
        public ActionResult Edit(int id)
        {
            GiangDay model = (from m in db.GiangDays
                               where m.id == id
                               select m).SingleOrDefault();

            IEnumerable<SelectListItem> listGiangVien = db.GiangViens.Select(c => new SelectListItem
            {
                Value = c.id.ToString(),
                Text = c.tengv.ToString()
            });
            IEnumerable<SelectListItem> listMonHoc = db.MonHocs.Select(c => new SelectListItem
            {
                Value = c.id.ToString(),
                Text = c.tenmh.ToString()
            });
            ViewData["GiangVienSelected"] = model.magv;
            ViewData["ListGiangVien"] = listGiangVien;
            ViewData["ListMonHoc"] = listMonHoc;

            return View(model);
        }

        //
        // POST: /GiangDay/Edit/5
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Edit(int id, GiangDay giangDay)
        {
            GiangDay model = (from m in db.GiangDays
                               where m.id == id
                               select m).SingleOrDefault();
            try
            {
                // TODO: Add update logic here
                model.magv = giangDay.magv;
                model.mamh = giangDay.mamh;
                model.ngaybatdau = giangDay.ngaybatdau;
                model.ghichu = giangDay.ghichu;                
                
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {                
                return View(model);
            }
        }

        //
        // GET: /GiangDay/Delete/5
        [Authorize(Roles = "0")]
        public ActionResult Delete(int id)
        {
            var model = (from h in db.GiangDays
                         where h.id == id
                         select h).Take(1).SingleOrDefault();
            db.GiangDays.DeleteOnSubmit(model);
            db.SubmitChanges();
            return RedirectToAction("Index");
        }

        //
        // POST: /GiangDay/Delete/5
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
